package com.hu.jie.mywallpager.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.hu.jie.mywallpager.R;
import com.hu.jie.mywallpager.bean.SeekList;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jie on 2016/7/8.
 */
public class SearchBannerView extends RelativeLayout implements ViewPager.OnPageChangeListener {

    private ViewPager vp_banner;
    private LinearLayout ll_banner_group;
    private List<SeekList.URLH> mDatas;
    private int mCount;
    private Context mContext;
    private BannerAdapter adapter;
    private DisplayImageOptions options;
    private List<View> mImgs;
    private int mLastIndex;

    public SearchBannerView(Context context) {
        this(context, null);
    }

    public SearchBannerView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SearchBannerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.view_search_banner, this);

        vp_banner = (ViewPager) findViewById(R.id.vp_banner);
        ll_banner_group = (LinearLayout) findViewById(R.id.ll_banner_group);

        adapter = new BannerAdapter();
        mDatas = new ArrayList<>();
        mImgs = new ArrayList<>();
        this.mContext = context;

        vp_banner.setAdapter(adapter);
        vp_banner.addOnPageChangeListener(this);

        options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .showImageOnFail(R.mipmap.ic_launcher)
                .showImageOnLoading(R.mipmap.ic_launcher)
                .showImageForEmptyUri(R.mipmap.ic_launcher)
                .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
                .build();
    }

    public void setDatas(List<SeekList.URLH> datas) {
        if (datas == null) {
            return;
        }
        mDatas.addAll(datas);
        //得到viewpager的页数，和指示标的数量
        mCount = mDatas.size() / 2;

        //1.准备ViewPager的数据源
        for (int i = 0; i < mCount; i++) {
            //得到ViewPager每个页
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_search_topic_vp, null);
            ImageView iv_1 = (ImageView) view.findViewById(R.id.l_iv_1);
            ImageView iv_2 = (ImageView) view.findViewById(R.id.l_iv_2);
            ImageLoader.getInstance().displayImage(mDatas.get(2 * 1).cover_path, iv_1, options);
            ImageLoader.getInstance().displayImage(mDatas.get(2 * 1 + 1).cover_path, iv_2, options);
            mImgs.add(view);
        }
        //2.设置添加指示点
        for (int i = 0; i < mCount; i++) {
            //创建出来一个ImageView对象，准备加载指示点
            ImageView point = new ImageView(mContext);
            //设置图片
            point.setImageResource(R.drawable.selector_point);
            //设置属性
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.setMargins(5,10,10,5);
            point.setLayoutParams(params);

            //初始化第一次点的状态
            if (i == 0){
                point.setEnabled(true);
            }else {
                point.setEnabled(false);
            }
            ll_banner_group.addView(point);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        //选中变绿
        ll_banner_group.getChildAt(position).setEnabled(true);
        //上一个位置灰化
        ll_banner_group.getChildAt(mLastIndex).setEnabled(false);
        //记录最新的位置
        mLastIndex = position;
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    public class BannerAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            return mDatas.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = mImgs.get(position);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mImgs.get(position));
        }
    }

}
